package com.fin.zw.aiqas.controller;


import com.fin.zw.aiqas.annotation.CurrentUser;
import com.fin.zw.aiqas.entity.Department;
import com.fin.zw.aiqas.model.component.TreeJson;
import com.fin.zw.aiqas.model.request.DepartmentRequest;
import com.fin.zw.aiqas.model.response.DepartmentResponse;
import com.fin.zw.aiqas.model.session.SessionModel;
import com.fin.zw.aiqas.service.DepartmentService;
import com.fin.zw.aiqas.util.TreeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@RestController
@RequestMapping("/department")
@Api(value = "机构管理", description = "机构管理")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;


    @GetMapping("/fetch")
    @ApiOperation(value = "查询所有的机构", notes = "查询所有的机构")
    public ResponseEntity<List<Department>> fetch(DepartmentRequest request,
                                                  @ApiIgnore @CurrentUser SessionModel sessionModel) {
        List<Department> list = departmentService.getDepartment(request, sessionModel);
        return ResponseEntity.ok().body(list);
    }

    @GetMapping("/fetchTree")
    @ApiOperation(value = "获取当前", notes = "查询所有的机构")
    public ResponseEntity<List<TreeJson>> fetchTree(DepartmentRequest request,
                                                    @ApiIgnore @CurrentUser SessionModel sessionModel) {
        List<Department> list = departmentService.getDepartment(request, sessionModel);
        List<TreeJson> result = TreeUtil.listToTree(list);
        return ResponseEntity.ok().body(result);
    }


    @GetMapping("/fetchList")
    @ApiOperation(value = "查询所有的机构", notes = "查询所有的机构")
    public ResponseEntity<List<DepartmentResponse>> fetch() {
        List<DepartmentResponse> list = departmentService.getDepartmentAll();
        return ResponseEntity.ok().body(list);
    }

    @PostMapping("add")
    @ApiOperation(value = "机构的添加", notes = "机构的添加")
    public ResponseEntity<Void> createDepartment(@RequestBody DepartmentRequest request,
                                                 @ApiIgnore @CurrentUser SessionModel sessionModel) {
        departmentService.createDepartment(request, sessionModel);
        return ResponseEntity.ok().build();
    }

    @PostMapping("update")
    @ApiOperation(value = "机构的添加", notes = "机构的添加")
    public ResponseEntity<Void> updateDepartment(@RequestBody DepartmentRequest request,
                                                 @ApiIgnore @CurrentUser SessionModel sessionModel) {
        departmentService.updateDepartment(request,sessionModel);
        return ResponseEntity.ok().build();
    }


    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "机构的删除", notes = "机构的删除")
    public ResponseEntity<Void> deleteDeparment(@PathVariable Long id) {
        departmentService.deleteDepartment(id);
        return ResponseEntity.ok().build();
    }
}
